const mixins = require('../../mixins/user')
const common = require('../../mixins/common')
const app = getApp();
const api = require('../../config/api');
const util = require('../../utils/util');
const { tagsHotUrl } = require('../../config/api');

const options = {

  /**
   * 页面的初始数据
   */

  data: {
    agree: false,
    is_agree: false,
    configData: {},
    video: false,
    btnState: false,
    is_register:false,
    is_register_hide:false,
    nickname:"",
    init_avatarUrl:"https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    avatarUrl:"",
  },
  confirm(){
    if(!this.data.avatarUrl || this.data.avatarUrl==""){
      wx.showToast({
        title: '请上传头像',
        icon: 'none'
      })
      return;
    }
    if(this.data.nickname==""){
      wx.showToast({
        title: '请填写昵称',
        icon: 'none'
      })
      return;
    }
    let datas = this.data.datas;
    datas.user_avatar = this.data.avatarUrl;
    datas.user_name = this.data.nickname;

    wx.showLoading({
      title: '请稍候'
    });
    util.request(api.updateInfoUrl, datas, "POST").then(function (res) {
      if (res.status) {
        wx.hideLoading();
        wx.showToast({
          title: '保存成功',
          icon: 'none',
          complete(){
            wx.navigateBack();
          }
        })
      }
    })

  },

  onCheckChange: function(e) {
    // console.log(e.detail.value[0]);
    if(e.detail.value.length == 0) {
      // console.log(e.detail.value.length)
      this.setData({
        agree: false,
        is_agree: false
      })
    } else {   //当用户点击了checkbox确定了，值为1
      console.log(e.detail.value.length)
      this.setData({
        agree: true,
        is_agree: true
      });
    }
  },

  addimage: function (e) {
    this.uploadPicturesPic(e.detail.url, "avatarUrl", e.currentTarget.dataset.type);
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      pic:avatarUrl,
      imagesubjectClipper:true
    })
  },
  skip(){
    this.setData({
      is_register_hide:false
    })
    setTimeout(()=>{
      wx.navigateBack();
    },300)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.configData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo != '') {
      wx.navigateBack();
    };
  },

  /***
   * 获取用户信息
   */
  getUserInfo: function (e) {
    if(this.data.agree == false) {
      wx.showToast({
        title: '请先阅读并勾选用户服务协议！',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    this.setData({
      btnState: true
    })
    this.loginByWeixin().then(res => {
      let datas =
      {
        user_avatar: res.user_avatar,
        user_name: res.user_name,
        user_introduce: res.user_introduce,
        user_birthday: res.user_birthday,
        user_background_maps: res.user_background_maps,
      }
      if(!res.is_register){
        //新用户，初次登录
        datas.user_introduce = "这个人很懒，什么都没有留下！";
        datas.user_birthday = "请选择";
        this.setData({
          is_register:true
        })
        setTimeout(()=>{
          this.setData({
            is_register_hide:true
          })
        },100)
      }else{
        wx.navigateBack();
        this.setData({
          btnState: false
        })
      }
      
      this.setData({
        datas
      })
    }).catch((err) => {
      console.log(err)
      this.setData({
        btnState: false
      })
    });
  },

  toTiaoKuan() {
    wx.navigateTo({
      url: '/pages/clause/clause?type=21',
    })
  },

  /**
   * 返回登陆
   */
  back: function () {
    wx.reLaunch({
      url: '/pages/tabbar/index/index',
    })
  }
}

mixins(options)
common(options)
Page(options)